package com.tudouji.project.weigh.service.impl;

import java.util.List;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.project.harvest.domain.Freightorder;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.service.impl.FreightorderServiceImpl;
import com.tudouji.project.weigh.domain.WeightInterfaceParam;
import com.tudouji.project.weigh.mapper.WeightInterfaceParamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.weigh.mapper.WeightDataMapper;
import com.tudouji.project.weigh.domain.WeightData;
import com.tudouji.project.weigh.service.IWeightDataService;

/**
 * 过磅记录接口Service业务层处理
 * 
 * @author tudouji
 * @date 2023-03-22
 */
@Slf4j
@Service
public class WeightDataServiceImpl implements IWeightDataService 
{
    @Autowired
    private WeightDataMapper weightDataMapper;
    @Autowired
    private WeightInterfaceParamMapper weightInterfaceParamMapper;
    @Autowired
    private FreightorderServiceImpl freightorderService;
    @Autowired
    private FreightorderMapper freightorderMapper;

    /**
     * 查询过磅记录接口
     * 
     * @param id 过磅记录接口ID
     * @return 过磅记录接口
     */
    @Override
    public WeightData selectWeightDataById(Long id)
    {
        return weightDataMapper.selectWeightDataById(id);
    }

    /**
     * 查询过磅记录接口列表
     * 
     * @param weightData 过磅记录接口
     * @return 过磅记录接口
     */
    @Override
    public List<WeightData> selectWeightDataList(WeightData weightData)
    {
        return weightDataMapper.selectWeightDataList(weightData);
    }

    /**
     * 新增过磅记录接口
     * 
     * @param weightData 过磅记录接口
     * @return 结果
     */
    @Override
    public int insertWeightData(WeightData weightData)
    {
        weightData.setCreateTime(DateUtils.getNowDate());
        return weightDataMapper.insertWeightData(weightData);
    }

    /**
     * 修改过磅记录接口
     * 
     * @param weightData 过磅记录接口
     * @return 结果
     */
    @Override
    public int updateWeightData(WeightData weightData)
    {
        weightData.setUpdateTime(DateUtils.getNowDate());
        return weightDataMapper.updateWeightData(weightData);
    }

    /**
     * 批量删除过磅记录接口
     * 
     * @param ids 需要删除的过磅记录接口ID
     * @return 结果
     */
    @Override
    public int deleteWeightDataByIds(Long[] ids)
    {
        return weightDataMapper.deleteWeightDataByIds(ids);
    }

    /**
     * 删除过磅记录接口信息
     * 
     * @param id 过磅记录接口ID
     * @return 结果
     */
    @Override
    public int deleteWeightDataById(Long id)
    {
        return weightDataMapper.deleteWeightDataById(id);
    }

    @Override
    public boolean addOrupdateWeightData(WeightData weightData) {
        WeightInterfaceParam param = weightInterfaceParamMapper.selectWeightInterfaceParamListOne();
        weightData.setBatch(param.getCode());
        if (null == weightData.getpTime()){//首次过磅数据
            WeightData data = new WeightData();
            data.setCode(weightData.getCode());
            data.setBatch(weightData.getBatch());
            List<WeightData> dataList = weightDataMapper.selectWeightDataList(data);
            if (dataList.size() == 0){
                return insertFreightOrder(weightData);
            }else{
                log.info("数据库中已存在同批次、流水号过磅数据: " + weightData.toString());
                return true;
            }
        }else{
            weightData.setWeight(weightData.getDzz());

            WeightData data = new WeightData();
            data.setCode(weightData.getCode());
            data.setBatch(weightData.getBatch());
            data = weightDataMapper.selectWeightData(data);
            if (null == data){
                log.info("已过磅完成的历史数据，不生成货运单！现在生成。");
//                return true;
                return insertFreightOrder(weightData);
            }
            //回皮数据送上来之后，系统从接口数据表中找到对应的货运单id，判断货运单状态尚未入库，则更新货运单hweight=净重。
            Freightorder order = freightorderMapper.selectFreightorderByAutoId(data.getOrderId());
            if (order.getStatus() > 80){
                log.info("货运单状态: " + order.getStatus() + "已入库, 不能更新净重！");
                return true;
            }
            Freightorder freightorder = new Freightorder();
            freightorder.setId(data.getOrderId());
            freightorder.setHweight(weightData.getWeight());
            freightorderMapper.updateFreightorder(freightorder);

            weightData.setId(data.getId());
            weightDataMapper.updateWeightData(weightData);
            return true;
        }

    }

    private boolean insertFreightOrder(WeightData weightData) {
        boolean isInsert = freightorderService.generateFreightOrder(weightData);
        weightData.setCreateTime(DateUtils.getNowDate());
        if (isInsert){
            weightDataMapper.insertWeightData(weightData);
        }
        return isInsert;
    }
}
